<template>
  <div class="register">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="150px" class="register-form">
      <h3 class="title">企业注册</h3>
      <el-divider />
      <el-form-item label="企业名称"  prop="registerNickName">
        <el-input v-model="registerForm.registerNickName" type="text" placeholder="请输入注册企业名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="registerAccount" class="toUpperCase">
        <el-input v-model="registerForm.registerAccount" type="text" placeholder="请输入统一社会信用代码" maxlength="18" show-word-limit />
      </el-form-item>
      <el-form-item label="联系人" prop="registerLxr">
        <el-input v-model="registerForm.registerLxr" type="text" placeholder="请输入注册联系人" maxlength="20" show-word-limit />
      </el-form-item>
      <el-form-item label="手机号码" prop="registerLxrPhone">
        <el-input v-model="registerForm.registerLxrPhone" type="text" placeholder="请输入联系人手机号码" maxlength="11" show-word-limit />
      </el-form-item>
      <el-form-item label="联系邮箱" prop="registerLxrEmail">
        <el-input v-model="registerForm.registerLxrEmail" type="text" placeholder="请输入联系人邮箱" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="设置密码" prop="password">
        <el-input v-model="registerForm.password" type="password" placeholder="请设置密码" maxlength="20" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请确认密码" maxlength="20" />
      </el-form-item>
      <el-form-item label="验证码" prop="code" v-if="captchaEnabled">
        <el-input v-model="registerForm.code" placeholder="验证码" style="width: 63%" />
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="medium" type="primary"  style="width:60%;" @click.native.prevent="handleRegister" >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
        <div style="padding-top: 10px;padding-right: 30px;float: right;">
          <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-register-footer">
      <span></span>
    </div>
  </div>
</template>

<script>
import {getCodeImg, getPublicKey} from "@/api/login";
import {compRegister} from "@/api/register";
import {encrypt} from '@/utils/jsencrypt'

export default {
  name: "compRegister",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        registerType: "COMP",
        registerNickName: "",
        registerAccount: "",
        registerLxr: "",
        registerLxrPhone: "",
        registerLxrEmail: "",
        registerPassword: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        registerNickName: [
          { required: true, trigger: "blur", message: "请输入注册企业名称" },
          { min: 2, max: 80, message: '企业名称长度必须介于 2 和 80 之间', trigger: 'blur' }
        ],
        registerAccount: [
          { required: true, trigger: "blur", message: "请输入注册企业统一社会信用代码" },
          {
            required: true,
            pattern: /^([0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\d{14})$/,
            message: '统一社会信用代码格式不正确',
            trigger: 'blur'
          },
        ],
        registerLxr: [
          { required: true, trigger: "blur", message: "请输入注册联系人" },
          { min: 2, max: 20, message: '联系人长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        registerLxrPhone: [
          { required: true, trigger: "blur", message: "请输入注册联系人手机" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        registerLxrEmail: [
          { required: true, trigger: "blur", message: "请输入注册联系人邮箱" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getPublicKey() {
      return new Promise((resolve, reject) => {
        getPublicKey()
          .then(res => {
            resolve(res)
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          this.getPublicKey().then(res=> {
            let publicKey = res;
            const encryptPassword = encrypt(this.registerForm.password, publicKey);
            this.registerForm.registerPassword = encryptPassword;
            compRegister(this.registerForm).then(res => {
              const registerNickName = this.registerForm.registerNickName;
              const registerAccount = this.registerForm.registerAccount;
              this.$alert("<font color='red'>" + registerNickName + " 恭喜你!<br/>您的账号 " + registerAccount + " 注册成功！</font>", '系统提示', {
                dangerouslyUseHTMLString: true,
                type: 'success'
              }).then(() => {
                this.$router.push("/login");
              }).catch(() => {
              });
            }).catch(() => {
              this.loading = false;
              if (this.captchaEnabled) {
                this.getCode();
              }
            })
          })
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../../assets/images/login-background.jpg");
  background-size: cover;
  .title {
    margin: 0px auto 30px auto;
    text-align: left;
    color: #000000;
  }
}

.register-form {
  border-radius: 6px;
  background: #ffffff;
  width: 600px;
  padding: 20px 25px 5px 25px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
.toUpperCase  {
  .el-input__inner {
    text-transform: uppercase !important;
  }
}
</style>
